﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Gamev1
{
    public class Button
    {
        private bool fade;
        public bool isClicked;
        public Color color;
        private MouseState prevMouseState;

        public Button()
        {
            color = Color.White;
        }
        
        public void Update(MouseState mouse)
        {
            MouseState currentMouseState = Mouse.GetState();
            Point mousePosition = new Point(mouse.X, mouse.Y);
            //Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
            //isClicked = false;
            if (Rectangle.Contains(mousePosition))
            {
                changeTransparency();
                if (isClicking(mouse))
                {
                    isClicked = true;
                }
                else
                {
                    isClicked = false;
                }
            }
            else if (color.A < 255)
            {
                color.A += 3;
                //isClicked = false;
            }
            prevMouseState = currentMouseState;
        }

        private Rectangle Rectangle
        {
            get
            {
                return new Rectangle(
                   (int)Position.X,
                   (int)Position.Y,
                   Texture.Width,
                   Texture.Height);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Position, color);
        }

        private bool isClicking(MouseState currentMouseState)
        {
            return currentMouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released;
        }

        public void changeTransparency()
        {
            if (color.A == 255)
            {
                fade = false;
            }
            if (color.A == 0)
            {
                fade = true;
            }
            if (fade)
            {
                color.A += 3;
            }
            else
            {
                color.A -= 3;
            }
        }

        public Texture2D Texture { get; set; }
        public Vector2 Position { get; set; }
        private Rectangle buttonRectangle { get; set; }
        public Rectangle ButtonRectangle
        {
            get
            {
                return new Rectangle(
                    (int)Position.X,
                    (int)Position.Y,
                    (int)Texture.Width,
                    (int)Texture.Height);
            }
        }
    }
}
